import reducer from '../src/reducer';
import {fromJS, List, Map} from 'immutable';
import * as folderStore from '../src/core';
import {expect} from 'chai';
describe('reducer', ()=>{
	var folderTree = {
        id: "1",
        name: "folder1",
        children: [{
            id: "11",
            parentId: "1",
            name: "folder11",
            children: [{
                parentId: "11",
                id: "111",
                name: "folder111",
            }, {
                parentId: "11",
                id: "112",
                name: "folder112",
            }]
        },{
        	id: "12",
            parentId: "1",
            name: "folder12",
            children: [{
                parentId: "12",
                id: "121",
                name: "folder121",
            }]
        }]
    }

	it('move to', ()=>{				
		const initialState = folderStore.setFolderTree(Map(), folderTree);		
		var obj = folderStore.getFolderById(initialState.get("folderTree"), "121");		
		var action = {type: 'MOVE_TO', node : obj, toId: '11' }
		const nextState = reducer(initialState, action);
		var folderTree1 = {
			id: "1",
			name: "folder1",
			children: [{
				id: "11",
				parentId: "1",
				name: "folder11",
				children: [{
					parentId: "11",
					id: "111",
					name: "folder111",
				}, {
					parentId: "11",
					id: "112",
					name: "folder112",
				}, {
					parentId: "12",
					id: "121",
					name: "folder121",
				}]
			}, {
				id: "12",
				parentId: "1",
				name: "folder12",
				children: []
			}]
		}

		expect(nextState.get("folderTree")).to.equal(fromJS(folderTree1));
	})

	it('copy to', ()=>{				
		const initialState = folderStore.setFolderTree(Map(), folderTree);		
		var obj = folderStore.getFolderById(initialState.get("folderTree"), "121");		
		var action = {type: 'COPY_TO', node : obj, toId: '11' }
		const nextState = reducer(initialState, action);
		var folderTree1 = {
			id: "1",
			name: "folder1",
			children: [{
				id: "11",
				parentId: "1",
				name: "folder11",
				children: [{
					parentId: "11",
					id: "111",
					name: "folder111",
				}, {
					parentId: "11",
					id: "112",
					name: "folder112",
				}, {
					parentId: "12",
					id: "121",
					name: "folder121",
				}]
			}, {
				id: "12",
				parentId: "1",
				name: "folder12",
				children: [{
					parentId: "12",
					id: "121",
					name: "folder121",
				}]
			}]
		}

		expect(nextState.get("folderTree")).to.equal(fromJS(folderTree1));
	})

	it('add', ()=>{				
		const initialState = folderStore.setFolderTree(Map(), folderTree);	
		var newNode = {
			parentId: "12",
			id: "121",
			name: "folder121",
		}
		var action = {type: 'ADD', node : newNode, toId: '11' }
		const nextState = reducer(initialState, action);
		var folderTree1 = {
			id: "1",
			name: "folder1",
			children: [{
				id: "11",
				parentId: "1",
				name: "folder11",
				children: [{
					parentId: "11",
					id: "111",
					name: "folder111",
				}, {
					parentId: "11",
					id: "112",
					name: "folder112",
				}, {
					parentId: "12",
					id: "121",
					name: "folder121",
				}]
			}, {
				id: "12",
				parentId: "1",
				name: "folder12",
				children: [{
					parentId: "12",
					id: "121",
					name: "folder121",
				}]
			}]
		}

		expect(nextState.get("folderTree")).to.equal(fromJS(folderTree1));
	})

	it('remove', ()=>{				
		const initialState = folderStore.setFolderTree(Map(), folderTree);			
		var newNode = {
			parentId: "12",
			id: "121",
			name: "folder121",
		}
		var action = {type: 'REMOVE', nodeId : "11"}
		const nextState = reducer(initialState, action);
		var folderTree1 = {
			id: "1",
			name: "folder1",
			children: [{
				id: "12",
				parentId: "1",
				name: "folder12",
				children: [{
					parentId: "12",
					id: "121",
					name: "folder121",
				}]
			}]
		}

		expect(nextState.get("folderTree")).to.equal(fromJS(folderTree1));
	})

	it('update', ()=>{				
		const initialState = folderStore.setFolderTree(Map(), folderTree);	
		
		var action = {type: 'UPDATE', nodeId : "11", node: {name: "folde11_1"}}
		const nextState = reducer(initialState, action);
		var folderTree1 = {
        id: "1",
        name: "folder1",
        children: [{
            id: "11",
            parentId: "1",
            name: "folde11_1",
            children: [{
                parentId: "11",
                id: "111",
                name: "folder111",
            }, {
                parentId: "11",
                id: "112",
                name: "folder112",
            }]
        },{
        	id: "12",
            parentId: "1",
            name: "folder12",
            children: [{
                parentId: "12",
                id: "121",
                name: "folder121",
            }]
        }]
    }

		expect(nextState.get("folderTree")).to.equal(fromJS(folderTree1));
	})
})

